<?php
/**
 * Created by PhpStorm.
 * User: 小灰灰
 * Date: 2023-02-28
 * Time: 13:55:46
 * Info: 表单html模板
 */

namespace libs;

use api\portal\controller\DatabaseController;
use ClassesWithParents\D;

class Form
{

    /**
     * 输出单文本html
     *
     * @param $name     表单name
     * @param $value    表单value
     * @param $tips     提示
     *
     * @return string
     */
    public static function text($name, $value = '', $tips = '')
    {
        $str = '';
        $str .= '<input type="text" name="'.$name.'" id="id_'.$name.'" autocomplete="off" placeholder="'.$tips.'" value="'.$value.'" class="layui-input">';

        return $str;
    }

    /**
     * 输出文本域html
     *
     * @param $name     表单name表单name
     * @param $value    表单value表单value
     * @param $tips     提示
     *
     * @return string
     */
    public static function textarea($name, $value = '', $tips = '')
    {
        $str = '';
        $str .= '<textarea class="layui-textarea" id="id_'.$name.'" name="'.$name.'" placeholder="'.$tips.'" >'.$value.'</textarea>';

        return $str;
    }

    /**
     * @param $name      表单name
     * @param $value     表单value
     * @param $list_arr  循环数据【key为：值， v为：名】
     *
     * @return void
     */
    public static function radio($name, $value = '', $list_arr = [])
    {
        $str = '';
        foreach ($list_arr as $key => $v) {
            $checked = '';
            if ($key == $value) {
                $checked = 'echo checked';
            }
            $str .= '<input type="radio" name="'.$name.'" value="'.$key.'"title="'.$v.'" '.$checked.'>';
        }

        return $str;
    }

    /**
     * @param $name      表单name
     * @param $value     表单value
     * @param $list_arr  循环数据【key为：值， v为：名】
     *
     * @return void
     */
    public static function select($name, $value = '', $list_arr = [])
    {
        $str = '';
        $str .= '<select name="'.$name.'">';
        foreach ($list_arr as $key => $v) {
            $selected = '';
            if ($key == $value) {
                $selected = 'echo selected';
            }
            $str .= '<option value="'.$key.'" '.$selected.'>'.$v.'</option>';
        }
        $str .= '</select>';

        return $str;
    }

    /**
     * 图片上传
     *
     * @param $name         name名
     * @param $value        值
     * @param $return_id    返回input表单的id名称
     * @param $is_check     是否显示选择图片按钮
     *
     * @return void
     */
    public static function image($name, $value = '', $return_id = '', $is_check = 0)
    {
        $str = '';
        $str .= '<input type="text" name="value[image]" value="'.$value.'" ';
        $str .= '          onmouseover="hui_img_preview(\'thumb-select-input\',this.value)"';
        $str .= '          onmouseout="layer.closeAll();" id="thumb-select-input" autocomplete="off"';
        $str .= '          class="layui-input">';
        $str .= '<button type="button" class="layui-btn layui-btn-normal layUpload" id="lay_pic"
                    data-input-id="'.$return_id.'" data-type="file">';
        $str .= '<i class="layui-icon"></i>上传图片';
        $str .= '</button>';
        if ($is_check) {
            $url_img = url('upload_file/index', ['type' => 'one', 'select_id' => ''.$return_id.'']);
            $str     .= '<button type="button" class="layui-btn ml-10" data-open-pic="'.$url_img.'">';
            $str     .= '<i class="layui-icon">&#xe67c;</i>选择图片';
            $str     .= '</button>';
        }

        return $str;
    }

    /**
     * 复选框
     *
     * @param $name
     * @param $value
     * @param $tips
     *
     * @return void
     */
    public static function checkbox($name, $value = '', $tips = '')
    {
        $str = '';
        $str .= '';

        return $str;
    }

    /**
     * 富文本编辑器
     *
     * @param $name
     * @param $value
     *
     * @return void
     */
    public static function editorOld($name, $editorId, $value = '', $style = '')
    {
        $string = '<script type="text/plain" class="editorHtml" id="'.$editorId.'" name="'.$name.'" style="'.$style.'" >'.$value.'</script>';

        return $string;
    }

    public static function editor($name, $editorId, $value = '', $style = '', $isload = false)
    {
        $val = htmlspecialchars_decode($value);
        $res = self::editorUEditorPlus($name, $editorId, $val, $style, false, $isload);

        return $res;
    }

    /**
     * 富文本编辑器 mini版本
     * @return void
     */
    public static function editorMini($name, $editorId, $value = '', $style = '', $isload = false)
    {
        $val = htmlspecialchars_decode($value);

        return self::editorUEditorPlus($name, $editorId, $val, $style, true, $isload);
    }

    public static function editorAiEditor()
    {
        return self::AiEditor();
    }

    /**
     * ueditorPlus
     * @return void
     */
    private static function editorUEditorPlus($name, $editorId, $val, $style, $isMini = false, $isload = false)
    {
        $toolbars = get_config('ueditor_icon');
        if ( ! empty($toolbars)) {
            $toolbars = array_filter(explode(',', $toolbars));
        }
        $toolbars1 = [];
        foreach ($toolbars as $key => $item) {
            if (is_int($key / 5) && $key > 0) {
                array_push($toolbars1, "|");
                array_push($toolbars1, $toolbars[$key]);
            } else {
                array_push($toolbars1, $toolbars[$key]);
            }
        }

        $version = '4.2.0';
        if ($isMini) {
            $editorId  = $editorId."_mini";
            $toolbars2 = 'toolbars:[[ "fullscreen","source","|","bold","italic","underline","forecolor",
            "removeformat", "formatmatch", "autotypeset","fontsize",
            "insertimage","insertcode","|","link","unlink","emotion","preview"]]';
        } else {
            if ( ! empty($toolbars1)) {
                $toolbars2 = 'toolbars:['.json_encode($toolbars1).']';
            } else {
                $toolbars2 = "";
            }
        }
        $libPath  = "/static".DIRECTORY_SEPARATOR."admin".DIRECTORY_SEPARATOR."lib".DIRECTORY_SEPARATOR;
        $configJs = $libPath.'ueditor-plus-'.$version.DIRECTORY_SEPARATOR.'ueditor.config.js';
        $Js2      = $libPath.'ueditor-plus-'.$version.DIRECTORY_SEPARATOR.'ueditor.all.js';
        $lang     = $libPath.'ueditor-plus-'.$version.DIRECTORY_SEPARATOR.'lang'.DIRECTORY_SEPARATOR.'zh-cn'.DIRECTORY_SEPARATOR.'zh-cn.js';
        $string   = '';
        if ($isload) {
            $string .= '<script type="text/javascript" src="'.$configJs.'"></script>';
            $string .= '<script type="text/javascript" src="'.$Js2.'"></script>';
            $string .= '<script type="text/javascript" src="'.$lang.'"></script>';
        }
        $string .= '<script type="text/plain" class="editorHtml" id="'.$editorId.'" name="'.$name.'" style="'.$style.'" >'.$val.'</script>';
        $string .= '<script type="text/javascript">';
        $string .= 'var ue = UE.getEditor("'.$editorId.'",{';
        $string .= $toolbars2;
        $string .= '});';
        $string .= '</script>';

        return $string;

    }

    private static function AiEditor()
    {
        $str = '<link type="text/css" rel="stylesheet" href="/static/admin/lib/aieditor-v1.3.6/style.css">';
        $str .= '<script type="module">
        import {AiEditor} from "/static/admin/lib/aieditor-v1.3.6/index.js"
        new AiEditor({
            element: "#aiEditor",
            placeholder: "点击输入内容...",
            content: "AiEditor 是一个面向 AI 的下一代富文本编辑器。<br>这编辑器咋样，你听我给你吹",
            ai: {
                models: {
                    openai: {
                        endpoint: "https://one.api4gpt.com",
                        model: "gpt-4o-mini",
                        apiKey: ""
                    }
                }
            },
        })
    </script>';
        $str .= '<div id="aiEditor" style="height: 550px;  margin: 20px"></div>';

        return $str;
    }

}
